//
//  WmcTeamMemberAlbumVC.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/16.
//

import MJRefresh
import UIKit

// MARK: - 扩展WmcTeamMemberAlbumVC + WmcNavigatorProtocol

extension WmcTeamMemberAlbumVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.teamMemberCloudAlbum]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcTeamMemberAlbumVM()
        if let viewModel = parms.context as? WmcTeamMemberAlbumVM {
            vm = viewModel
        }
        let vc = WmcTeamMemberAlbumVC(vm)
        return vc
    }
}

// MARK: - 扩展WmcTeamMemberAlbumVC + WmcVMProtocol

extension WmcTeamMemberAlbumVC: WmcVMProtocol {
    typealias VMType = WmcTeamMemberAlbumVM
}

class WmcTeamMemberAlbumVC: WmcBaseVC, WmcAlbumTransitionProtocol {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        if viewModel.type == .memberPhoto {
            title = "成员照片"
        } else {
            title = viewModel.title
        }
        let vm = WmcAlbumPhotoListVM()
        vm.member = viewModel.member
        vm.teamAblum = viewModel.teamAblum
        vm.watermark = viewModel.watermark
        vm.watermarkId = viewModel.watermarkId

        if viewModel.type == .memberPhoto {
            vm.type = .memberPhoto
            vm.browserTitle = "成员照片"
        }
        if viewModel.type == .watermarkPhoto {
            vm.type = .watermarkPhoto
            vm.browserTitle = viewModel.title
        }
        let vc = WmcAlbumPhotoListVC(vm: vm)
        addChidVC(vc: vc)
    }

    /// 添加子控制器
    /// - Parameter vc: 子控制器
    /// - Returns: description
    func addChidVC(vc: WmcAlbumPhotoListVC) {
        addChild(vc)
        view.addSubview(vc.view)
        vc.view.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        vc.didMove(toParent: self)
    }

    // Internal

    override func makeUI() {
        makeNavView(showShadow: false)
        // title = "成员照片"
        view.backgroundColor = UIColor.wmc_gray_F7F8FA

        navView.addSubview(shareBtn)
        shareBtn.snp.makeConstraints { make in
            make.centerY.equalTo(navView.backImageView)
            make.right.equalTo(-Wmc_standardEdge.right)
        }

        var isShowedGuide = false
        if let isShowed: Bool = WmcUserdefault.object(forKey: .Tips.CloudAlbum.shareBubbles) {
            isShowedGuide = isShowed
        }

        if !isShowedGuide {
            showTakePhotoBubble()
        }
    }

    override func bindViewAction() {
        shareBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            WmcUserdefault.save(value: true, forKey: .Tips.CloudAlbum.shareBubbles)
            weakSelf.bubbleView?.dismiss()
            WmcHudManager.loading()
            weakSelf.viewModel.fetchShare { model in
                WmcHudManager.dismiss()
                if model.url.isEmpty {
                    WmcHudManager.toast("分享失败")
                    return
                }
                var shareTitle = ""
                if let remark = weakSelf.viewModel.member?.remark {
                    shareTitle = remark + "的照片"
                } else {
                    shareTitle = model.title
                }
                let shareAlertView = WmcShareAlertView()
                let sharedConfig: WmcShareAlertView.ShareType.Config = .init(title: shareTitle, description: model.content, thumbImage: UIImage.YP.AppIcon) { completed in
                    if completed {
                        shareAlertView.dismiss()
                    }
                }
                shareAlertView.shareType = .link(model.url, config: sharedConfig)
                shareAlertView.show()

            } failure: { error in
                WmcHudManager.dismiss()
                if let error = error {
                    WmcHudManager.toast(error)
                } else {
                    WmcHudManager.toast("网络请求失败")
                }
            }
        }
    }

    // Private

    private lazy var shareBtn: WmcLeftImgRightTextBtn = {
        let btn = WmcLeftImgRightTextBtn(title: "分享", image: "share".wmc_image)
        btn.setTintColor(.wmc_mainTheme)
        btn.font = 16.wmc_font
        btn.imgSize = 20.wmc_autoHor
        return btn
    }()

    private var bubbleView: WmcBubbleView?

    private func showTakePhotoBubble() {
        let title = "照片可以批量分享了"
        let config = WmcBubbleVM.ContentStyle.TextConfig(textColor: .wmc_white_FFFFFF, font: 18.wmc_font, textAlignment: .center)

        let bubbleVM = WmcBubbleVM()
        bubbleVM.contentStyle = .text(title, config: config)
        bubbleVM.closeType = .close
        bubbleVM.closeStyle = .line
        bubbleVM.useCloseBtn = true
        bubbleVM.bubbleSize = CGSize(width: 220, height: 44)
        bubbleVM.contentCornerRadius = 8
        bubbleVM.markView = shareBtn
        bubbleVM.showInView = view
        bubbleVM.showDirection = .bottom
        bubbleVM.markViewCenterOffset = -80
        bubbleVM.arrowOffset = -bubbleVM.markViewCenterOffset
        bubbleView = WmcBubbleView(bubbleVM)
        bubbleView?.show()
        bubbleView?.closeClosure = {
            WmcUserdefault.save(value: true, forKey: .Tips.CloudAlbum.shareBubbles)
        }
    }
}
